// inventory.go
package main

import (
	"fmt"
	"log"
	"strings"
)

type Inventory struct {
	items map[Portable]int
}

func NewInventory() *Inventory {
	return &Inventory{items: make(map[Portable]int)}
}

func (this Inventory) searchEntity(name string) Portable {
	for item, _ := range this.items {
		if item.Name() == name {
			return item
		}
	}
	return nil
}

// todo: testen, ob das ding überhaupt drin is
func (this *Inventory) Unbag(item Portable, number int) {
	if this.items[item] < number {
		number = this.items[item]
	}
	this.items[item] -= number
	if this.items[item] == 0 {
		delete(this.items, item)
	}
	return
}

func (this *Inventory) Bag(item Portable, number int) {
	log.Printf("Bag(): Putting %v items with name '%v' into Inventory", number, item.Name())
	this.items[item] += number
	log.Printf("Bag(): Inventory is now: %v", this.items)
}

func (this Inventory) checkInventory() string {

	itemList := []string{}
	for item, number := range this.items {
		itemList = append(itemList, fmt.Sprintf("%d %s", number, item.Name()))
	}
	return fmt.Sprintf("Your Inventory: %s", strings.Join(itemList, ", "))
}
